package com.zhipu.resetbalance.pop.update;

import android.content.Context;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.lxj.xpopup.core.CenterPopupView;
import com.zhipu.resetbalance.R;
import com.zhipu.resetbalance.listener.ChooseClickCallback;
import com.zhipu.resetbalance.mobile.temporaryClass.VersionUpdate;
import com.zhipu.resetbalance.utils.ApkController;
import com.zhipu.resetbalance.utils.CommonUtils;
import com.zhipu.resetbalance.utils.InitializeUtils;
import com.zhipu.resetbalance.view.NumberProgressBar;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

public class UpdatePopup extends CenterPopupView {
    /**
     *
     */
    private VersionUpdate mVersionUpdate;

    /**
     *
     */
    private NumberProgressBar mNumberProgressBar;

    /**
     *
     */
    private Button mBtnOk;

    public UpdatePopup(@NonNull Context context) {
        super(context);
    }

    public UpdatePopup(@NonNull Context context,VersionUpdate mVersionUpdate) {
        super(context);
        this.mVersionUpdate = mVersionUpdate;
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_updata;

    }


    @Override
    protected void onCreate() {
        super.onCreate();
        //
        TextView versionName = findViewById(R.id.tv_title);
        versionName.setText("是否升级到"+ mVersionUpdate.versionName +"版本?");
        //
        TextView explain = findViewById(R.id.tv_update_info);
        explain.setText(mVersionUpdate.explain);
        //
        mNumberProgressBar = findViewById(R.id.NumberProgressBar);
        //
        mBtnOk = findViewById(R.id.BtnOk);
        mBtnOk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                mBtnOk.setVisibility(View.GONE);
                //
                mNumberProgressBar.setVisibility(View.VISIBLE);
                //
                File appDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
                if (!appDir.exists()) {
                    appDir.mkdirs();
                }
                String appPath = appDir + File.separator + "Balance/APP/";
                String fileNam  = CommonUtils.getApkName();
                download(mVersionUpdate.url, appPath, fileNam, new ChooseClickCallback() {
                    @Override
                    public void onChooseClickCallback(int mInt) {
                        if(mInt == -1){
                            mBtnOk.setVisibility(VISIBLE);
                            mNumberProgressBar.setVisibility(GONE);
                        }else {
                            InitializeUtils.getDefaultMaker().show("正在安装");
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    String path = appPath + fileNam;
                                    AppUtils.installApp(path);
//                                    if (ApkController.install(path, ActivityUtils.getTopActivity())) {
//                                        InitializeUtils.getDefaultMaker().show("安装成功");
//                                    } else {
//                                        InitializeUtils.getDefaultMaker().show("安装失败");
//                                    }
                                }
                            }).start();
                            dismiss();
                        }
                    }
                });
            }
        });
    }


    /**
     * 下载
     * @param url
     * @param path
     * @param fileName
     * @param mChooseClickCallback
     */
    public void download(String url, String path, String fileName, ChooseClickCallback mChooseClickCallback){
        OkHttpUtils.get().url(url).build().execute(new FileCallBack(path, fileName) {

            @Override
            public void inProgress(float progress, long total, int id) {
                setNumberProgressBar(progress);
                super.inProgress(progress, total, id);
            }

            @Override
            public void onError(Call call, Response response, Exception e, int id) {
                mChooseClickCallback.onChooseClickCallback(-1);
            }

            @Override
            public void onResponse(File response, int id) {
                InitializeUtils.getDefaultMaker().show("下载完成");
                mChooseClickCallback.onChooseClickCallback(0);
            }
        });
    }


    /**
     * 设置进度
     */
    public void setNumberProgressBar(Float progress) {
        if (null != mNumberProgressBar) {
            mNumberProgressBar.setProgress(Math.round(progress * 100));
            mNumberProgressBar.setMax(100);
        }
    }
}
